﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using ComputerStoreManager.View;
using Modules.Core;

namespace ComputerStoreManager.ViewModel {
    public class ManagerWindowViewModel : BaseViewModel {

        #region Variables
        private bool m_isVisibleScrollBar = false;
        private MenuControl m_menuControl;
        private MenuControlViewModel m_menuControlViewModel;

        private HomeControl m_homeControl;

        private EmployeeControl m_employeeControl;
        //private EmployeeControlViewModel m_employeeControlViewModel;
        private AddEmployeeControl m_addEmpControl;
        private ViewEmployeeControl m_viewEmpControl;

        private CustomerControl m_customerControl;
        //private CustomerControlViewModel m_customerControlViewModel;
        private AddCustomerControl m_addCusControl;
        private ViewCustomerControl m_viewCusControl;

        private ProductControl m_productControl;
        //private ProductControlViewModel m_productControlViewModel;
        private AddProductControl m_addProductControl;
        private ViewProductControl m_viewProductControl;

        private InvoiceControl m_invoiceControl;
        private AddInvoiceControl m_addInvoiceControl;
        private ViewInvoiceBuyControl m_viewInvoiceControl;
        #endregion

        #region Properties
        public bool IsVisibleScrollBar {
            get {
                return m_isVisibleScrollBar;
            }
            set {
                m_isVisibleScrollBar = value; OnPropertyChanged("IsVisibleScrollBar");
            }
        }
        public Grid TopGrid {
            get;
            set;
        }
        public Grid LeftGrid {
            get;
            set;
        }
        public Grid RightGrid {
            get;
            set;
        }
        #endregion

        #region Constructor
        public ManagerWindowViewModel(Grid m_topGrid, Grid m_leftGrid, Grid m_rightGrid) {
            TopGrid = m_topGrid;
            LeftGrid = m_leftGrid;
            RightGrid = m_rightGrid;

            ///Load top menu lên
            InitTopMenuControl();

            //Load những gì liên quan đến Employee
            InitEmployeeControl();

            //Load những gì liên quan đến Customer
            InitCustomerControl();

            //Load những gì liên quan đến Product
            InitProductControl();

            //load những gì liên quan đến Invoice
            InitInvoiceControl();

            //Load những gì liên quan đến Home
            InitHomeControl();

            //m_addEmpControl = new AddEmployeeControl();
            //m_viewEmpControl = new ViewEmployeeControl();
            //m_homeControl = new HomeControl();
            
            //LeftGrid.Children.Add(m_employeeControl);
            //LeftGrid.Children.Add(m_homeControl);
            //RightGrid.Children.Add(m_addEmpControl);
            //RightGrid.Children.Add(m_viewEmpControl);

        }

        #endregion

        #region Invoice
        private void InitInvoiceControl() {
            m_invoiceControl = new InvoiceControl();
            m_invoiceControl.OnAddInvoiceSellClicked += m_invoiceControl_OnAddInvoiceSellClicked;
            m_invoiceControl.OnViewInvoiceSellClicked += m_invoiceControl_OnViewInvoiceSellClicked;

            m_addInvoiceControl = new AddInvoiceControl();
            m_viewInvoiceControl = new ViewInvoiceBuyControl();

            m_invoiceControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_addInvoiceControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_viewInvoiceControl.LayoutRoot.Visibility = Visibility.Collapsed;

            LeftGrid.Children.Add(m_invoiceControl);
            RightGrid.Children.Add(m_addInvoiceControl);
            RightGrid.Children.Add(m_viewInvoiceControl);
        }

        void m_invoiceControl_OnViewInvoiceSellClicked() {
            IsVisibleScrollBar = true;
            m_addInvoiceControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_viewInvoiceControl.LayoutRoot.Visibility = Visibility.Visible;
        }

        void m_invoiceControl_OnAddInvoiceSellClicked() {
            IsVisibleScrollBar = false;
            m_addInvoiceControl.LayoutRoot.Visibility = Visibility.Visible;
            m_viewInvoiceControl.LayoutRoot.Visibility = Visibility.Collapsed;
        }
        #endregion

        #region Product

        private void InitProductControl() {
            m_addProductControl = new AddProductControl();
            m_viewProductControl = new ViewProductControl();

            m_productControl = new ProductControl();
            m_productControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_productControl.OnAddProductClicked += m_productControl_OnAddProductClicked;
            m_productControl.OnViewProductClicked += m_productControl_OnViewProductClicked;

            m_addProductControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_viewProductControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_productControl.LayoutRoot.Visibility = Visibility.Collapsed;

            LeftGrid.Children.Add(m_productControl);
            RightGrid.Children.Add(m_addProductControl);
            RightGrid.Children.Add(m_viewProductControl);
        }

        void m_productControl_OnViewProductClicked() {
            m_viewProductControl.ReloadData();
            IsVisibleScrollBar = true;
            m_addProductControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_viewProductControl.LayoutRoot.Visibility = Visibility.Visible;
        }

        void m_productControl_OnAddProductClicked() {
            IsVisibleScrollBar = false;
            m_viewProductControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_addProductControl.LayoutRoot.Visibility = Visibility.Visible;
        }

        #endregion

        #region Home

        private void InitHomeControl() {
            HomeControlViewModel vm = new HomeControlViewModel();
            //vm.OnMyAccountInfoClicked += vm_OnMyAccountInfoClicked;
            vm.OnViewCustomerClicked += vm_OnViewCustomerClicked;
            vm.OnViewEmpClicked += vm_OnViewEmpClicked;
            vm.OnViewInvoiceSellClicked += vm_OnViewInvoiceSellClicked;
            vm.OnViewProductClicked += vm_OnViewProductClicked;

            m_homeControl = new HomeControl(vm);
            m_homeControl.VisibleHome(true);
            //vm.OnMyAccountInfoClicked += 
            //m_homeControl = new HomeControl();
            //m_homeControl.VisibleHome(true);
            //m_homeControl.On

            LeftGrid.Children.Add(m_homeControl);
        }

        void vm_OnViewProductClicked() {
            HideAllComponen();
            m_homeControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_productControl.LayoutRoot.Visibility = Visibility.Visible;
            m_productControl_OnViewProductClicked();
        }

        void vm_OnViewInvoiceSellClicked() {
            HideAllComponen();
            m_homeControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_invoiceControl.LayoutRoot.Visibility = Visibility.Visible;
            m_invoiceControl_OnViewInvoiceSellClicked();
        }

        void vm_OnViewEmpClicked() {
            HideAllComponen();
            m_homeControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_employeeControl.LayoutRoot.Visibility = Visibility.Visible;
            m_employeeControl_OnViewEmpClicked();
        }

        void vm_OnViewCustomerClicked() {
            HideAllComponen();
            m_homeControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_customerControl.LayoutRoot.Visibility = Visibility.Visible;
            m_customerControl_OnViewCustomerClicked();
        }

        //void vm_OnMyAccountInfoClicked() {
        //    throw new NotImplementedException();
        //}
        #endregion

        #region Customer
        private void InitCustomerControl() {
            m_addCusControl = new AddCustomerControl();
            m_viewCusControl = new ViewCustomerControl();

            //m_customerControlViewModel = new CustomerControlViewModel();
            m_customerControl = new CustomerControl();
            m_customerControl.OnAddCustomerClicked += m_customerControl_OnAddCustomerClicked;
            m_customerControl.OnViewCustomerClicked += m_customerControl_OnViewCustomerClicked;

            m_addCusControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_viewCusControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_customerControl.LayoutRoot.Visibility = Visibility.Collapsed;

            LeftGrid.Children.Add(m_customerControl);
            RightGrid.Children.Add(m_addCusControl);
            RightGrid.Children.Add(m_viewCusControl);
        }

        void m_customerControl_OnViewCustomerClicked() {
            m_viewCusControl.ReloadData();
            IsVisibleScrollBar = true;
            m_addCusControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_viewCusControl.LayoutRoot.Visibility = Visibility.Visible;
        }

        void m_customerControl_OnAddCustomerClicked() {
            IsVisibleScrollBar = false;
            m_viewCusControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_addCusControl.LayoutRoot.Visibility = Visibility.Visible;
        }
        #endregion

        #region Employee

        private void InitEmployeeControl() {
            m_addEmpControl = new AddEmployeeControl();
            m_viewEmpControl = new ViewEmployeeControl();

            //m_employeeControlViewModel = new EmployeeControlViewModel();
            m_employeeControl = new EmployeeControl();
            m_employeeControl.OnAddEmpClicked += m_employeeControl_OnAddEmpClicked;
            m_employeeControl.OnViewEmpClicked += m_employeeControl_OnViewEmpClicked;

            m_addEmpControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_viewEmpControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_employeeControl.LayoutRoot.Visibility = Visibility.Collapsed;

            LeftGrid.Children.Add(m_employeeControl);
            RightGrid.Children.Add(m_addEmpControl);
            RightGrid.Children.Add(m_viewEmpControl);
        }

        private void InitTopMenuControl() {
            m_menuControlViewModel = new MenuControlViewModel();
            m_menuControlViewModel.OnHomeCommandClicked += m_menuControlViewModel_OnHomeCommandClicked;
            m_menuControlViewModel.OnEmployeeCommandClicked += m_menuControlViewModel_OnEmployeeCommandClicked;
            m_menuControlViewModel.OnInvoiceCommandClicked += m_menuControlViewModel_OnInvoiceCommandClicked;
            m_menuControlViewModel.OnCustomerCommandClicked += m_menuControlViewModel_OnCustomerCommandClicked;
            m_menuControlViewModel.OnProductCommandClicked += m_menuControlViewModel_OnProductCommandClicked;
            m_menuControlViewModel.OnStatisticsCommandClicked += m_menuControlViewModel_OnStatisticsCommandClicked;
            m_menuControlViewModel.OnExitCommandClicked += m_menuControlViewModel_OnExitCommandClicked;
            m_menuControl = new MenuControl(m_menuControlViewModel);

            TopGrid.Children.Add(m_menuControl);
        }

        void m_menuControlViewModel_OnExitCommandClicked() {
            //throw new NotImplementedException();
            MainWindow.GetInstance().Hide();
            LoginPanel.GetInstance().Show();
        }

        void m_employeeControl_OnViewEmpClicked() {

            IsVisibleScrollBar = true;
            m_addEmpControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_viewEmpControl.LayoutRoot.Visibility = Visibility.Visible;
        }

        void m_employeeControl_OnAddEmpClicked() {
            IsVisibleScrollBar = false;
            m_viewEmpControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_addEmpControl.LayoutRoot.Visibility = Visibility.Visible;
        }

        void m_employeeControlViewModel_OnViewEmployeeClicked() {
            MessageBox.Show("ok");
        }
        #endregion

        #region TopMenu

        void m_menuControlViewModel_OnStatisticsCommandClicked() {
            MessageBox.Show("Đang xây dựng");
        }

        void m_menuControlViewModel_OnProductCommandClicked() {
            HideAllComponen();
            m_productControl.LayoutRoot.Visibility = Visibility.Visible;
        }

        void m_menuControlViewModel_OnCustomerCommandClicked() {
            HideAllComponen();
            m_customerControl.LayoutRoot.Visibility = Visibility.Visible;
        }

        void m_menuControlViewModel_OnInvoiceCommandClicked() {
            HideAllComponen();
            m_invoiceControl.LayoutRoot.Visibility = Visibility.Visible;
        }

        void m_menuControlViewModel_OnEmployeeCommandClicked() {
            HideAllComponen();
            m_employeeControl.LayoutRoot.Visibility = Visibility.Visible;
            // m_homeControl.test(false);
        }

        void m_menuControlViewModel_OnHomeCommandClicked() {
            HideAllComponen();
            m_homeControl.VisibleHome(true);
            //.test(true);
        }

        #endregion

        #region Methods
        private void HideAllComponen() {
            m_customerControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_viewCusControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_addCusControl.LayoutRoot.Visibility = Visibility.Collapsed;

            m_employeeControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_viewEmpControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_addEmpControl.LayoutRoot.Visibility = Visibility.Collapsed;

            m_productControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_addProductControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_viewProductControl.LayoutRoot.Visibility = Visibility.Collapsed;

            m_invoiceControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_addInvoiceControl.LayoutRoot.Visibility = Visibility.Collapsed;
            m_viewInvoiceControl.LayoutRoot.Visibility = Visibility.Collapsed;

            m_homeControl.LayoutRoot.Visibility = Visibility.Collapsed;

        }
        #endregion
    }
}
